package com.lenovo.agricultureclient.request;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;

import com.lenovo.agricultureclient.util.Util;
/**
 * 找回密码请求
 * @author zongbingwu
 *
 */
public class FindPwdRequest extends AgricultureRequest 
{
	//action name
	private static final String ACTION = "findPassword"; 
	//用户名
	private String username;
	//用户邮箱
	private String email;

	@Override
	protected String getActionName() {
		return ACTION;
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	//jason协议时的获取body函数
	@Override
	protected String onGetJasonBody() 
	{
		JSONObject jsonObj = new JSONObject();
		try 
		{
			jsonObj.put("username", username);
			return jsonObj.toString();
		} 
		catch (JSONException e) {
			e.printStackTrace();
		}
		return super.onGetJasonBody();
	}
	
	//soap协议时的获取body函数
	@Override
	protected String onGetSoapBody() 
	{
		String soapBody = Util.getXmlElementStr("username", username);
		return Util.getSoapXml(soapBody);
	}

	//jason协议时解析回应函数
	@Override
	protected void onJasonParese(String responseStr) 
	{
		try 
		{
			JSONObject jsonRespObj = new JSONObject(responseStr);
			if(jsonRespObj != null)
			{
				if(jsonRespObj.has("result"))
				{
					String result = jsonRespObj.getString("result");
					result = result.toLowerCase();
					//判断请求是否成功
					if(result.equals("ok")){
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				}
				//从回应中解析出用户邮箱
				if(jsonRespObj.has("email"))
				{
					email = jsonRespObj.getString("email");
				}
			}
		} 
		catch (JSONException e) 
		{
			isSuccess = false;
			e.printStackTrace();
		}
	}
	
	//soap协议时解析回应函数
	@Override
	protected void onSoapParese(String responseStr) 
	{
		String result = "";
		//从完整的soap协议内容中解析出soap body内容
		Element soapBodyEle = Util.getSoapBodyElement(responseStr);
		if(soapBodyEle != null)
		{
			result = Util.getChildElementValueStr(soapBodyEle, "result");
			result = result.toLowerCase();
			//从回应中解析出用户邮箱
			email = Util.getChildElementValueStr(soapBodyEle, "email");
		}
		//判断请求是否成功
		if(result.equals("ok")){
			isSuccess = true;
		} else {
			isSuccess = false;
		}
	}
	
}
